package com.borya.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


public class ReadExcel {
    public static void main(String[] args) {
//        ReadExcel obj = new ReadExcel();
//        // 此处为我创建Excel路径：E:/zhanhj/studysrc/jxl下
//        File file = new File("D://download/qq/入库模板/联通卡号x.xls");
//        List<String> excelList = obj.readExcel(file);
//        for (int i = 0; i < excelList.size(); i++) {
//            System.out.println(excelList.get(i));
//        }

    }
    // 去读Excel的方法readExcel，该方法的入口参数为一个File对象
    public static List<String> readExcel(File file) {
        try {
            // 创建输入流，读取Excel
            InputStream is = new FileInputStream(file.getAbsolutePath());
            // jxl提供的Workbook类
            Workbook wb = Workbook.getWorkbook(is);
            // Excel的页签数量
            List<String> outerList=new ArrayList<>();
            // 每个页签创建一个Sheet对象
            Sheet sheet = wb.getSheet(0);
            // sheet.getRows()返回该页的总行数
            StringBuilder sb = new StringBuilder();
            for (int i = 0,rows = sheet.getRows(); i < rows; i++) {
            	sb.delete(0, sb.length());
                // sheet.getColumns()返回该页的总列数
                for (int j = 0, columns = sheet.getColumns(); j < columns; j++) {
                	if(j > 0){
                		sb.append(",");
                	}
                    String cellinfo = sheet.getCell(j, i).getContents();
                    sb.append(cellinfo);
                }
                outerList.add(sb.toString());
            }
            return outerList;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}